package com.kingbook.api.rest;

import com.kingbook.core.annotations.ContainProperties;
import com.kingbook.core.auth.LoginManager;
import com.kingbook.core.result.JSONResult;
import com.kingbook.services.SpendService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

import static com.kingbook.core.auth.LoginManager.AUTH;

/**
 * @Author Bucky
 * @Date 2020-03-30 21:18:59
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("spend")
public class SpendRest {

    @Autowired
    SpendService spendService;

    @PutMapping
    @ContainProperties({"id", "amount", "payDate"})
    public JSONResult save(@RequestBody Map<String, Object> param, @RequestHeader(name = AUTH) String auth) {
        param.put("mid", LoginManager.getMid(auth));
        log.info("---->{}", param);
        int id = MapUtils.getIntValue(param, "id");
        return id > 0 ? spendService.modify(param) : spendService.save(param);
    }

    @DeleteMapping("{id}")
    public JSONResult remove(@PathVariable int id, @RequestHeader(name = AUTH) String auth) {
        int mid = LoginManager.getMid(auth);
        log.info("---->mid={}", mid);
        return spendService.remove(id, mid);
    }

    @GetMapping("{id}")
    public JSONResult find(@PathVariable int id, @RequestHeader(name = AUTH) String auth) {
        int mid = LoginManager.getMid(auth);
        log.info("---->mid={}", mid);
        return spendService.find(id, mid);
    }

    @PostMapping("query")
    public JSONResult query(@RequestBody Map<String, Object> param, @RequestHeader(name = AUTH) String auth) {
        param.put("mid", LoginManager.getMid(auth));
        log.info("---->{}", param);
        return spendService.query(param);
    }

    @PostMapping("pages")
    @ContainProperties({"pageNo", "pageSize"})
    public JSONResult queryForPages(@RequestBody Map<String, Object> param, @RequestHeader(name = AUTH) String auth) {
        param.put("mid", LoginManager.getMid(auth));
        log.info("---->{}", param);
        return spendService.queryForPages(param);
    }

}
